<template>
  <view class="login-popup" v-if="visible">
    <view class="popup-mask" @click="handleCancel"></view>
    <view class="popup-content">
      <view class="popup-header">
        <text class="popup-title">微信登录</text>
        <view class="close-icon" @click="handleCancel">
          <text>×</text>
        </view>
      </view>
      <view class="popup-body">
        <image class="wechat-logo" src="/static/wechat-logo.png" mode="aspectFit"></image>
        <text class="login-tip">请使用微信账号登录</text>
        <text class="login-desc">授权后可体验更多功能</text>
      </view>
      <view class="popup-footer">
        <button class="login-btn" open-type="getUserInfo" @getuserinfo="handleLogin">
          <image class="btn-icon" src="/static/wechat-icon.png" mode="aspectFit"></image>
          <text>微信一键登录</text>
        </button>
        <view class="cancel-btn" @click="handleCancel">暂不登录</view>
      </view>
    </view>
  </view>
</template>

<script>
import wechatUtil from '../utils/wechat.js';

export default {
  name: 'WechatLoginPopup',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 组件内部数据
    }
  },
  methods: {
    /**
     * 处理微信登录
     */
    handleLogin(e) {
      if (e.detail.errMsg === 'getUserInfo:ok') {
        wechatUtil.login((userInfo) => {
          // 登录成功，通知父组件
          this.$emit('login-success', userInfo);
          this.$emit('update:visible', false);
          
          uni.showToast({
            title: '登录成功',
            icon: 'success'
          });
        }, (err) => {
          // 登录失败，通知父组件
          this.$emit('login-fail', err);
        });
      } else {
        // 用户拒绝授权
        uni.showToast({
          title: '您拒绝了授权',
          icon: 'none'
        });
        this.$emit('login-cancel');
      }
    },
    
    /**
     * 处理取消登录
     */
    handleCancel() {
      this.$emit('update:visible', false);
      this.$emit('login-cancel');
    }
  }
}
</script>

<style lang="scss">
.login-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  
  .popup-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.6);
  }
  
  .popup-content {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 600rpx;
    background-color: #FFFFFF;
    border-radius: 20rpx;
    overflow: hidden;
    box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.1);
    
    .popup-header {
      position: relative;
      padding: 30rpx 0;
      text-align: center;
      border-bottom: 1rpx solid #F5F5F5;
      
      .popup-title {
        font-size: 32rpx;
        font-weight: 500;
        color: #333333;
      }
      
      .close-icon {
        position: absolute;
        right: 20rpx;
        top: 20rpx;
        width: 40rpx;
        height: 40rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        
        text {
          font-size: 40rpx;
          color: #999999;
          line-height: 1;
        }
      }
    }
    
    .popup-body {
      padding: 40rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      
      .wechat-logo {
        width: 120rpx;
        height: 120rpx;
        margin-bottom: 30rpx;
      }
      
      .login-tip {
        font-size: 32rpx;
        color: #333333;
        margin-bottom: 10rpx;
      }
      
      .login-desc {
        font-size: 26rpx;
        color: #999999;
      }
    }
    
    .popup-footer {
      padding: 0 40rpx 40rpx;
      
      .login-btn {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 88rpx;
        background-color: #07C160;
        border-radius: 44rpx;
        margin-bottom: 20rpx;
        
        .btn-icon {
          width: 40rpx;
          height: 40rpx;
          margin-right: 10rpx;
        }
        
        text {
          font-size: 30rpx;
          color: #FFFFFF;
        }
      }
      
      .cancel-btn {
        text-align: center;
        font-size: 28rpx;
        color: #666666;
        padding: 10rpx 0;
      }
    }
  }
}
</style>
